package example.conc;

import java.util.LinkedList;

/**
 * zhu.tan@gmail.com
 * 18-Nov-2009
 */
public class BlockingQueuePreJava5 {
    private final LinkedList queue = new LinkedList();

    /**
     * This method pushes an object onto the end of the queue, and
     * then notifies one of the waiting threads.
     */
    public void push(Object o) {
        synchronized (queue) {
            queue.add(o);
            queue.notifyAll();
        }
    }

    /**
     * The pop operation blocks until either an object is returned
     * or the thread is interrupted, in which case it throws an
     * InterruptedException.
     */
    public Object pop() throws InterruptedException {
        synchronized (queue) {
            while (queue.isEmpty()) {
                queue.wait();
            }
            return queue.removeFirst();
        }
    }

    /**
     * Return the number of elements currently in the queue.
     */
    public int size() {
        synchronized (queue) {
            return queue.size();
        }
    }
}
